/******************************************************************************
 * Copyright 2024 zhangcong. All Rights Reserved.
 *****************************************************************************/
#pragma once

#include <string>
#include <vector>
#include <map>
#include <thread>
#include <memory>
#include "business/recordbusiness/proto/sensor_proto.h"
#include "business/recordbusiness/file/store_file.h"
#include "common/sensordata.h"
#include "nlohmann/json.hpp"

using json = nlohmann::json;

#define MAX_FIG_BUFF_LEN 50 * 1024
#define MAX_PLC_BUFF_LEN 1024
#define MAX_RAW_BUFF_LEN 10 * 1024

struct StoreTaskInfo
{
  std::string storeTaskName;
  std::string coreCpuID;
  std::string storeCpuID;
  std::string zmqIp;
  std::string mccAddress;
  std::vector<std::string> sn;
  std::vector<std::string> chParam;
  std::string startTime;
  std::string endTime;
  std::string createTime;
  std::string wsUrl;
  std::string minioEndPoint;
  std::string minioBucketName;
  std::string minioAccessKey;
  std::string minioSecretKey;
  std::string jsonInfo;
  bool upload_file;
  bool upload_data;
};

class StoreTask
{
public:
  StoreTask(const StoreTaskInfo &info);
  ~StoreTask();

public:
  void StartTask();
  void PauseTask();

private:
  void ListenFigDataFunc();
  void ListenRawDataFunc();
  void ListenPlcDataFunc();
  void AssignSensorData(const figureData &figure, const rawData &raw, 
    SensorDataInfo::figureInfo &sensorInfo);
  void addSensorData(const SensorDataInfo::CommonSensor& sensorData, const std::string& sensorType);
  void addFile( const std::string& sensorType);
  void readJsonFile(const std::string &filename, json &objjson);

private:
  StoreTaskInfo m_task_info;
  std::shared_ptr<SensorProtoManager> m_proto_manager;
  bool m_ListenFigFlag;
  std::unique_ptr<std::thread> m_ListenFigThread;
  bool m_ListenPlcFlag;
  std::unique_ptr<std::thread> m_ListenPlcThread;
  bool m_ListenRawFlag;
  std::unique_ptr<std::thread> m_ListenRawThread;
  uint8_t m_SensorBuffer[MAX_FIG_BUFF_LEN];
  int32_t m_SensorRecvLen;
  uint8_t m_PlcBuffer[MAX_PLC_BUFF_LEN];
  int32_t m_PlcRecvLen;
  uint8_t m_RawBuffer[MAX_RAW_BUFF_LEN];
  int32_t m_RawRecvLen;
  std::map<std::string, std::shared_ptr<StoreFile>> m_fileList;
  json  m_data_enable_;
};
